package com.dsdl.chatapp.module.service;

import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.dsdl.chatapp.contacts.Constant;
import com.dsdl.chatapp.data.SocketMessage;
import com.dsdl.chatapp.data.source.local.ChatPersistenceContract;
import com.dsdl.chatapp.common.db.ChatDAO;
import com.dsdl.chatapp.common.utils.AESUtil;
import com.dsdl.chatapp.common.utils.LogUtil;
import com.dsdl.chatapp.common.utils.StringUtils;
import com.dsdl.chatapp.common.utils.socket.ISocketManager;
import com.dsdl.chatapp.common.utils.socket.NewSocketManager;
import com.google.gson.Gson;

/**
 * Created by zzg on 2016/8/12.
 * 聊天服务
 */
public class ChatService extends Service implements ISocketManager.ReceiveListener{
    private static String TAG = ChatService.class.getSimpleName();


    @Override
    public void onCreate() {
        super.onCreate();
        connectSocket();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 建立Socket连接及消息接收器
     */
    private void connectSocket(){
        NewSocketManager.getInstance().connect(this);
    }

    @Override
    public void receiverSuccess(String msgResponse) {
        if (!StringUtils.isEmpty(msgResponse) && msgResponse.length() >= 0){
            try {
                String decodeStr = AESUtil.decode(msgResponse);
                if (decodeStr == null){
                    return;
                }
                SocketMessage chatMsg = new Gson().fromJson(decodeStr,SocketMessage.class);
                if (chatMsg == null){
                    return;
                }
                if ("system".equals(chatMsg.getMsgType())){
                    return;
                }
                ContentValues values = new ContentValues();
                values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_FROM_USER,chatMsg.getFromUser());
                values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_TO_USER,chatMsg.getToUser());
                values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_CREATE_TIME,chatMsg.getCreateTime());
                values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_CONTENT,chatMsg.getContent());
                values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_MSG_ID,chatMsg.getMsgId());
                values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_MSG_TYPE,chatMsg.getMsgType());
                if (chatMsg.getMsgId() != null){
                    ChatDAO.getInstance().insertOrUpdate(values,ChatPersistenceContract.ChatEntry.COLUMN_NAME_MSG_ID+" = ?",new String[]{chatMsg.getMsgId()});
                }
                sendBroadcast(new Intent(Constant.CHAT_WINDOW_FILTER).putExtra("chat_message",chatMsg));
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
